/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.util.UnitSymbolProcessor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.comac.ins.isfd.domain.bo.IsfdDataUnitSystemManagementBo;
import com.comac.ins.isfd.domain.vo.IsfdDataUnitSystemManagementVo;
import com.comac.ins.isfd.domain.IsfdDataUnitSystemManagement;
import com.comac.ins.isfd.mapper.IsfdDataUnitSystemManagementMapper;
import com.comac.ins.isfd.service.IIsfdDataUnitSystemManagementService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据属性单位管理Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-18
 */
@RequiredArgsConstructor
@Service
public class IsfdDataUnitSystemManagementServiceImpl implements IIsfdDataUnitSystemManagementService {


    private final IsfdDataUnitSystemManagementMapper baseMapper;

    /**
     * 查询数据属性单位管理
     *
     * @param id 主键
     * @return 数据属性单位管理
     */
    @Override
    public IsfdDataUnitSystemManagementVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询数据属性单位管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 数据属性单位管理分页列表
     */
    @Override
    public TableDataInfo<IsfdDataUnitSystemManagementVo> queryPageList(IsfdDataUnitSystemManagementBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdDataUnitSystemManagement> lqw = buildQueryWrapper(bo);
        Page<IsfdDataUnitSystemManagementVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的数据属性单位管理列表
     *
     * @param bo 查询条件
     * @return 数据属性单位管理列表
     */
    @Override
    public List<IsfdDataUnitSystemManagementVo> queryList(IsfdDataUnitSystemManagementBo bo) {
        LambdaQueryWrapper<IsfdDataUnitSystemManagement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdDataUnitSystemManagement> buildQueryWrapper(IsfdDataUnitSystemManagementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdDataUnitSystemManagement> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUnitSystem()), IsfdDataUnitSystemManagement::getUnitSystem, bo.getUnitSystem());
        lqw.like(StringUtils.isNotBlank(bo.getUnitEnglishName()), IsfdDataUnitSystemManagement::getUnitEnglishName, bo.getUnitEnglishName());
        lqw.like(StringUtils.isNotBlank(bo.getUnitChineseName()), IsfdDataUnitSystemManagement::getUnitChineseName, bo.getUnitChineseName());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitSymbol()), IsfdDataUnitSystemManagement::getUnitSymbol, bo.getUnitSymbol());
        lqw.eq(StringUtils.isNotBlank(bo.getAssociatedQuantity()), IsfdDataUnitSystemManagement::getAssociatedQuantity, bo.getAssociatedQuantity());
        lqw.like(StringUtils.isNotBlank(bo.getUnitDescription()), IsfdDataUnitSystemManagement::getUnitDescription, bo.getUnitDescription());
        lqw.eq(IsfdDataUnitSystemManagement::getIsDelete, 0);
        return lqw;
    }

    /**
     * 新增数据属性单位管理
     *
     * @param bo 数据属性单位管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdDataUnitSystemManagementBo bo) {
        IsfdDataUnitSystemManagement add = MapstructUtils.convert(bo, IsfdDataUnitSystemManagement.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Integer insertByBoList(List<IsfdDataUnitSystemManagementBo> boList) {
        int successCount = 0;
        for (IsfdDataUnitSystemManagementBo bo : boList) {
            IsfdDataUnitSystemManagementBo queryBo = new IsfdDataUnitSystemManagementBo();
            bo.setUnitSymbol(UnitSymbolProcessor.processUnitExpression(bo.getUnitSymbol()));
            queryBo.setUnitSystem(bo.getUnitSystem());
            queryBo.setUnitEnglishName(bo.getUnitEnglishName());
            queryBo.setAssociatedQuantity(bo.getAssociatedQuantity());
            IsfdDataUnitSystemManagement existUnit = baseMapper.selectOne(buildQueryWrapper(queryBo));
            if (existUnit != null & bo.getId() == null) {
                continue; // 如果单位已存在，跳过当条数据
            }
            boolean success = baseMapper.insertOrUpdate(MapstructUtils.convert(bo, IsfdDataUnitSystemManagement.class));
            if (success) {
                successCount++;
            }
        }

        return successCount;
    }

    /**
     * 修改数据属性单位管理
     *
     * @param bo 数据属性单位管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdDataUnitSystemManagementBo bo) {
        IsfdDataUnitSystemManagement update = MapstructUtils.convert(bo, IsfdDataUnitSystemManagement.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdDataUnitSystemManagement entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除数据属性单位管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
